package luj.tool.jnproxyan.proxy.stats.http.history;

import io.netty.handler.codec.http.HttpObject;
import io.netty.handler.codec.http.HttpRequest;
import luj.tool.jnproxyan.proxy.stats.http.history.state.HttpHistoryDat;
import luj.tool.jnproxyan.proxy.stats.http.history.state.HttpHistoryItem;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 代理开始实际处理网页请求时更新
 */
@Component
public class History021ReqUpdater {

  public History021ReqUpdater inject(HttpHistoryDat historyDat, Long socksId, HttpObject httpObj) {
    var result = new History021ReqUpdater();
    result._historyDat = historyDat;
    result._socksId = socksId;
    result._httpObj = httpObj;
    result._curItemGetter = _curItemGetter;
    return result;
  }

  public void update() {
    if (!(_httpObj instanceof HttpRequest)) {
      return;
    }

    HttpHistoryItem curItem = _curItemGetter.inject(_historyDat, _socksId).getCurrent();
    var reqObj = (HttpRequest) _httpObj;

    curItem.setUri(reqObj.uri());
  }

  HttpHistoryDat _historyDat;

  Long _socksId;
  HttpObject _httpObj;

  @Autowired
  HistoryCurItemGetter _curItemGetter;
}
